<?php

/**
 * This is the model class for table "user_deal".
 *
 * The followings are the available columns in table 'user_deal':
 * @property string $id
 * @property string $user_id
 * @property string $deal_id
 * @property string $date
 *
 * The followings are the available model relations:
 * @property Deal $deal
 * @property User $user
 */
class UserDeal extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserDeal the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_deal';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, deal_id', 'required'),
			array('user_id, deal_id', 'length', 'max'=>10),
			array('date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, deal_id, date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'deal' => array(self::BELONGS_TO, 'Deal', 'deal_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'deal_id' => 'Deal',
			'date' => 'Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('deal_id',$this->deal_id,true);
		$criteria->compare('date',$this->date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getByUserDeal($userId, $dealId) {
		return UserDeal::model()->findByAttributes(array('user_id'=>$userId, 'deal_id'=>$dealId));
	}
	
	public static function setCoupon($userId, $dealId) {
		$model = new UserDeal;
		$model->user_id = $userId;
		$model->deal_id = $dealId;
		$model->save(false);
		
		$model->code = $model->createCode();
		$model->save(false);
		
		return $model;
	}
	
	private function createCode() {
		$idlen = strlen($this->id);
		if($idlen <= 6) // if id less then 6 add microtime
			$code = $this->id . (substr(microtime(), -(6 - $idlen)));
		else
			$code = substr($this->id, -6);
			
		return $code;
			
	}
		
}